import JJUtil from "./JJUtil.js";

export default class JJStringUtil {

    static checkPhoneNumber(phoneNum) {
        if (JJUtil.isNull(phoneNum)) {
            return false;
        }
        const num = phoneNum.replace(/\s/g, "");
        if (num.length == 0) {
            return false;
        } else if (num.length != 11) {
            return false;
        }
        let myReg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|17[1-9]{1}|(18[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
        return myReg.test(num);
    }

    static securityPhoneNumber(phoneNum) {
        if (JJUtil.isNull(phoneNum) || phoneNum.length != 11) {
            return '';
        }

        let num = phoneNum.slice(0);
        let first = num.substr(0, 3)
        let last = num.substr(-4, 4)
        num = first + "****" + last;
        return num;
    }

    /**
     * 身份证号校验
     * string   要校验的字符串
     */
    static idCardVerify(idNum) {
        if (JJUtil.isNull(idNum)) {
            return false;
        }
        let MyRegExp = new RegExp(/(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/);
        if (idNum.replace(/\s/g, "").length != '18') {
            return false;
        }
        if (!MyRegExp.test(idNum.replace(/\s/g, ""))) {
            return false;
        }
        return true;
    }

    /**
     * 身份证号强校验
     * string   要校验的字符串
     */
    static checkID(val) {
        if (JJStringUtil.checkCode(val)) {
            var date = val.substring(6, 14);
            if (JJStringUtil.checkDate(date)) {
                if (JJStringUtil.checkProv(val.substring(0, 2))) {
                    return true;
                }
            }
        }
        return false;
    }


    /**
     * 身份证号 -> 省级地址码校验
     * val   要校验的字符串
     */
    static checkProv(val) {
        var pattern = /^[1-9][0-9]/;
        var provs = { 11: "北京", 12: "天津", 13: "河北", 14: "山西", 15: "内蒙古", 21: "辽宁", 22: "吉林", 23: "黑龙江 ", 31: "上海", 32: "江苏", 33: "浙江", 34: "安徽", 35: "福建", 36: "江西", 37: "山东", 41: "河南", 42: "湖北 ", 43: "湖南", 44: "广东", 45: "广西", 46: "海南", 50: "重庆", 51: "四川", 52: "贵州", 53: "云南", 54: "西藏 ", 61: "陕西", 62: "甘肃", 63: "青海", 64: "宁夏", 65: "新疆", 71: "台湾", 81: "香港", 82: "澳门" };
        if (pattern.test(val)) {
            if (provs[val]) {
                return true;
            }
        }
        return false;
    }

    /**
     * 身份证号 -> 出生日期码校验
     * val   要校验的字符串
     */
    static checkDate(val) {
        var pattern = /^(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)$/;
        if (pattern.test(val)) {
            var year = val.substring(0, 4);
            var month = val.substring(4, 6);
            var date = val.substring(6, 8);
            var date2 = new Date(year + "-" + month + "-" + date);
            if (date2 && date2.getMonth() == (parseInt(month) - 1)) {
                return true;
            }
        }
        return false;
    }

    /**
     * 身份证号 -> 校验码校验
     * val   要校验的字符串
     */
    static checkCode(val) {
        var p = /^[1-9]\d{5}(18|19|20)\d{2}((0[1-9])|(1[0-2]))(([0-2][1-9])|10|20|30|31)\d{3}[0-9Xx]$/;
        var factor = [7, 9, 10, 5, 8, 4, 2, 1, 6, 3, 7, 9, 10, 5, 8, 4, 2];
        var parity = [1, 0, 'X', 9, 8, 7, 6, 5, 4, 3, 2];
        var code = val.substring(17);
        if (p.test(val)) {
            var sum = 0;
            for (var i = 0; i < 17; i++) {
                sum += val[i] * factor[i];
            }
            if (parity[sum % 11] == code.toUpperCase()) {
                return true;
            }
        }
        return false;
    }
    /**
     * 身份证有效期截取
     */
    static certNoSplit(string) {
        if (JJUtil.isNull(string)) {
            return '';
        }

        try {
            let arr = string.split('-');
            let arr1 = arr[1].split('.');
            if (arr1.length < 2) {
                return arr1[0];
            }
            return arr1[0] + '年' + arr1[1] + '月' + arr1[2] + '日';
        } catch (error) {
            return '';
        }
    }

    //去掉所有空格
    static clearSpace(str) {
        if (JJUtil.isNull(str)) {
            return '';
        }
        return str.replace(/\s/g, '');
    }

    /**
     * 脱敏功能
     * str:字符串 beginStr:开始位置 endStr:结束位置
     */
    static desensitization(str, beginStr, endStr) {
        if (JJUtil.isNull(str)) {
            return '';
        }

        let len = str.length;
        let leftStr = str.substring(0, beginStr);
        let rightStr = str.substring(endStr, len);
        let string = '';
        let i = 0;
        try {
            for (i = 0; i < endStr - beginStr; i++) {
                string = string + '*';
            }
        } catch (error) {
            return '';
        }
        string = leftStr + string + rightStr;
        return string;
    }

    /**
     * 身份证格式化
     */
    static formatIdCard(idCard) {
        if (JJUtil.isNull(idCard)) {
            return "";
        }

        idCard = JJStringUtil.clearSpace(idCard);//去掉空格

        if (idCard.length <= 6) {
            return idCard;
        } else if (idCard.length <= 7) {
            let idCardText = idCard.slice(0, 6) + " " + idCard.slice(6);//第一次加空格特殊处理
            return idCardText;
        } else {
            let idCardText = idCard.slice(0, 6);
            let idCardModify = JJStringUtil.formatBankCard(idCard.slice(6, idCard.length));//利用银行卡输入规则
            return idCardText + ' ' + idCardModify;
        }
    }

    /**
     * 银行卡格式化
     */
    static formatBankCard(bankCard) {
        if (JJUtil.isNull(bankCard)) {
            return "";
        }

        if (bankCard.length <= 4) {
            return bankCard;
        } else {
            bankCard = JJStringUtil.clearSpace(bankCard);//去掉空格
            let bankCardText = bankCard.slice(0, 4);
            let bankCardModify = '';
            for (let i = 4; i < bankCard.length; i++) {
                if (i % 4 == 0) {
                    if (i != 4) {
                        bankCardModify = bankCardText.slice(0, i + i / 4 - 1);//取出需要空格前大半段字符串
                        bankCardText = bankCardModify + " " + bankCard.slice(i);//加空格并且拼接上最后一个字符串
                    } else {
                        bankCardText += " " + bankCard.slice(i);//第一次加空格特殊处理
                    }
                }
            }
            return bankCardText;
       }
    }

    /**
     * 姓名校验
     */
    static nameVerify(string) {
        if (JJUtil.isNull(string)) {
            return false;
        }

        if (string.substring(0, 1) == '·' || string.substring(string.length - 1, string.length) == '·') {
            return false;
        }

        if (string.indexOf(" ") != -1) {
            return false;
        }

        let strExp = new RegExp(/^[\u4E00-\u9FA5\s·]+$/);
        if (strExp.test(string)) {
            return true;
        } else {
            return false;
        }
    }

    /**
     * 金额格式化
     * s:金额 n:保留几位小数
     */
    static moneyFormat(s, n) {
        n = n > 0 && n <= 20 ? n : 2;
        s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
        let l = s.split(".")[0].split("").reverse(),
            r = s.split(".")[1],
            t = "";
        for (let i = 0; i < l.length; i++) {
            t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
        }
        return t.split("").reverse().join("") + "." + r;
    }


    /**
     * oldNum 旧数据
     * changeNum 新数据
     * cb用于结束的回调
     */
    static formatPhone(changeNum) {
        if (JJUtil.isNull(changeNum)) {
            return "";
        }
        let plainText = JJStringUtil.clearSpace(changeNum);//没有空格
        if (plainText.length >= 4 && plainText.length <= 7) {
            changeNum = JJStringUtil.phoneFormat(plainText);
        } else if (plainText.length >= 8) {
            changeNum = JJStringUtil.phoneFormat(plainText);
        } else {
            changeNum = plainText;
        }
        return changeNum;
    }

    //手机号格式化
    static phoneFormat(phone) {
        if (JJUtil.isNull(phone)) {
            return "";
        }
        let plainText = JJStringUtil.clearSpace(phone);
        if (plainText.length <= 3) {
            return plainText;
        }
        let fisrt = plainText.slice(0, 3);
        let middle = plainText.slice(3);
        let formatMiddle = JJStringUtil.phoneFormatMethd(middle);
        return fisrt + " " + formatMiddle;
    }

    //手机号格式化中间4位分割
    static phoneFormatMethd(phone4) {
        if (JJUtil.isNull(phone4)) {
            return "";
        }
        if (phone4.length <= 4) {
            return phone4;
        }
        return phone4.slice(0, 4) + (phone4.slice(4) == "" ? "" : (" " + JJStringUtil.phoneFormatMethd(phone4.slice(4))));
    }

    /**
     * 手机号校验
     * mobile 传入手机号
     */
    static mobileCheck(mobile) {
        if (JJUtil.isNull(mobile)) {
            return '';
        }

        let myreg = /^(((13[0-9]{1})|(14[0-9]{1})|(15[0-9]{1})|(16[0-9]{1})|17[1-9]{1}|(18[0-9]{1})|(19[0-9]{1}))+\d{8})$/;
        if (mobile.replace(/\s/g, "").length == 0) {
            return false;
        }
        if (mobile.replace(/\s/g, "").length != 11) {
            return false;
        }
        if (!myreg.test(mobile.replace(/\s/g, ""))) {
            return false;
        }

        return true;
    }

    /**
     * 校验银行卡号
     */
    static bankCardVerify(string) {
        if (JJUtil.isNull(string)) {
            return false;
        }

        if (string.replace(/\s/g, "").length == 0) {
            return false;
        }
        if (string.replace(/\s/g, "").length < 14 || string.replace(/\s/g, "").length > 25) {
            return false;
        }
        return true;
    }

    /**
     * CVV2 校验
     * string 要校验的字符串
     */
    static cvv2Verify(string) {
        if (JJUtil.isNull(string)) {
            return false;
        }

        if (string.length != 3) {
            return false;
        }

        let reg = new RegExp(/^[0-9]{3}$/);
        let verify = reg.test(string);

        return verify;
    }

    /**
     * 判断验证码为数字 且为6位
     * str 要校验的字符串
     */
    static vertifyCode(str) {
        let reg = new RegExp(/^\d{6}$/);
        return reg.test(str);
    }

    static moneyFormat(s, n) {
        n = n > 0 && n <= 20 ? n : 2;
        s = parseFloat((s + "").replace(/[^\d\.-]/g, "")).toFixed(n) + "";
        let l = s.split(".")[0].split("").reverse(),
            r = s.split(".")[1],
            t = "";
        for (let i = 0; i < l.length; i++) {
            t += l[i] + ((i + 1) % 3 == 0 && (i + 1) != l.length ? "," : "");
        }
        return t.split("").reverse().join("") + "." + r;
    }

    static checkChinese(obj_val) {
        var reg = new RegExp("[\\u4E00-\\u9FFF]+", "g");
        if (reg.test(obj_val)) {
            return true;
        } else {
            return false;
        }
    }

    static vertifyPassswordFormat(text) { // 判断密码是否是包含字母、数字、字符任意两种的6--20位字符串
        const reg = new RegExp(/(?!^[0-9]+$)(?!^[A-z]+$)(?!^[^A-z0-9]+$)^.{6,20}$/);
        const result = reg.test(text);
        return result;
    }

    // 判断字符是否是数字
    static checkIsInteger(str) {
        let regNum = new RegExp(/^(\-?)(\d+)$/);
        if (str == "")
            return true;
        if (regNum.test(str))
            return true;
        else
            return false; 
    }
}
